import { Module } from 'vuex'
import { ISongListInfoState } from './type'
import { IRootState } from '../types'

import { getSongsDetail, getSongInfo } from '@/service/album/album'
const songListInfoModule: Module<ISongListInfoState, IRootState> = {
  namespaced: true,
  state() {
    return {
      playlist: '',
      songInfo: ''
    }
  },
  mutations: {
    changeSongListInfo(state, infoRes) {
      state.playlist = infoRes.playlist
    },
    changeSongList(state, songInfo) {
      state.songInfo = songInfo
    }
  },
  actions: {
    async getSongListInfo({ commit }, id: number) {
      const infoRes = await getSongsDetail(id)
      const playListIds: any[] = []
      infoRes.privileges.map((item: { id: any }) => {
        return playListIds.push(item.id)
      })

      // const playListRes = await getLikeSong(ID)
      const randomS = Math.floor(Math.random() * 10)
      const randomE = Number(randomS + 8)
      const listArr = playListIds.slice(randomS, randomE)
      const { songs: songListInfo } = await getSongInfo(listArr)
      // const test = await getSongText(listArr)

      const songInfo: {
        songName: any
        singer: any
        singID: any
        singAl: any
        time: any
        mv: any
      }[] = []
      songListInfo.map((item: any) => {
        return songInfo.push({
          songName: item.name,
          singer: item.ar[0].name,
          singID: item.id,
          singAl: item.al,
          time: item.publishTime,
          mv: item.mv
        })
      })
      commit('changeSongList', songInfo)
      commit('changeSongListInfo', infoRes)
    }
  },
  getters: {}
}
export default songListInfoModule
